package com.example.demo.service.impl;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;

import com.example.demo.service.IService;

import tk.mybatis.mapper.common.Mapper;

public class BaseServiceImpl<T> implements IService<T> {

	@Autowired
	protected Mapper<T> mapper;
	
    public Mapper<T> getMapper() {
        return mapper;
    }
	
	@Override
	public T selectByPrimaryKey(Object key) {
		return mapper.selectByPrimaryKey(key);
	}

    @Override
    public T selectOne(T entity) {
        T t = null;
        t = mapper.selectOne(entity);
        return t;
    }

    @Override
    public int selectCount(T entity) {
        return mapper.selectCount(entity);
    }

    @Override
    public List<T> select(T entity) {
        return mapper.select(entity);
    }

    @Override
    public int save(T entity) {
        return mapper.insert(entity);
    }

    @Override
    public int deleteByPrimaryKey(Object key) {
        return mapper.deleteByPrimaryKey(key);
    }

    @Override
    public int delete(T entity) {
        return mapper.delete(entity);
    }

    @Override
    public int updateAll(T entity) {
        return mapper.updateByPrimaryKey(entity);
    }

    @Override
    public int updateNotNull(T entity) {
        return mapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public List<T> selectByExample(Object example) {
        return mapper.selectByExample(example);
    }

    // TODO 其他...

    @Override
    public <T> void initModelAdd(T model, String user_id) {
        Date date = new Date(); // 获取当前时间
        Class clazz = model.getClass();
        Field[] fields = null;
        if (clazz != null)
            fields = clazz.getDeclaredFields();
        Field rowId;
        try {
            // 行编号
            rowId = clazz.getDeclaredField("rowId");
            if (user_id != null)
                rowId.setAccessible(true);
            rowId.set(model, UUID.randomUUID().toString().replace("-", ""));
            // 创建时间
            Field createOn = model.getClass().getDeclaredField("createOn");
            if (createOn != null) {
                createOn.setAccessible(true);
                createOn.set(model, date);
            }
            // 创建人
            Field createBy = model.getClass().getDeclaredField("createBy");
            if (createBy != null) {
                createBy.setAccessible(true);
                createBy.set(model, user_id);
            }
            // 修改时间
            Field modifiedOn = model.getClass().getDeclaredField("modifiedOn");
            if (modifiedOn != null) {
                modifiedOn.setAccessible(true);
                modifiedOn.set(model, date);
            }
            // 修改人
            Field modifiedBy = model.getClass().getDeclaredField("modifiedBy");
            if (modifiedBy != null) {
                modifiedBy.setAccessible(true);
                modifiedBy.set(model, user_id);
            }
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public <T> void initModelUpdate(T model, String user_id) {
        Date date = new Date(); // 获取当前时间
        Class clazz = model.getClass();
        Field modifiedBy;
        try {
            modifiedBy = clazz.getDeclaredField("modifiedBy");

            if (modifiedBy != null) {
                modifiedBy.setAccessible(true);
                modifiedBy.set(model, user_id);

            }
            Field modifiedOn = clazz.getDeclaredField("modifiedOn");
            if (modifiedOn != null) {
                modifiedOn.setAccessible(true);
                modifiedOn.set(model, date);
            }
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
